package com.huaxin.hxcloud.work;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.RepoContentDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.Log;
import com.huaxin.common.utils.SpacesItemDecoration;
import com.huaxin.common.utils.fileread.FileDisplayActivity;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author lwjing
 * @date 2018/5/25
 * @Desc 知识库内容
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxLDRepositoryContentActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.et_search_input)
    EditText     et_search;
    @BindView(R.id.img_search_icon)
    ImageView    search_icon;
    @BindView(R.id.rl_plan_content)
    RecyclerView rl_plan;
    private LDRepoContentAdapter ldRepoContentAdapter;
    private List<RepoContentDTO.RepoContent> contentDatas = new ArrayList<>();
    private int    repoId;
    private View   emptyView;
    private String title;

    public static void start(Context context, int repoId, String title) {
        Intent intent = new Intent(context, HxLDRepositoryContentActivity.class);
        intent.putExtra(Constants.IK_REPO_ID, repoId);
        intent.putExtra(Constants.TITLE_KEY, title);
        context.startActivity(intent);
    }


    @Override
    protected void getBundleExtras(Bundle extras) {
        if (extras.containsKey(Constants.IK_REPO_ID)) {
            repoId = extras.getInt(Constants.IK_REPO_ID);
        } else {
            finish();
        }
        title = extras.getString(Constants.TITLE_KEY);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.act_ldrepo_content;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        mToolbar.setTitle(title);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rl_plan.setLayoutManager(manager);
        ldRepoContentAdapter = new LDRepoContentAdapter(R.layout.item_repo_content, contentDatas);
        rl_plan.setAdapter(ldRepoContentAdapter);
        rl_plan.addItemDecoration(new SpacesItemDecoration(30));
        //空布局
        emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) rl_plan.getParent(), false);

        ldRepoContentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //不同文件类型预览 文件夹查看
                RepoContentDTO.RepoContent repoContent = contentDatas.get(position);
                if (repoContent.getType() == 0) {
                    //文件 文件下载路径预览文件
                    String fileUrl = repoContent.getAddress();
                    Log.e("lwjing", "fileUrl=" + fileUrl);
                    //TODO 需要申请权限
                    String[] perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    FileDisplayActivity.show(HxLDRepositoryContentActivity.this, fileUrl, repoContent.getTexttype());

                } else {
                    //文件夹 根据文件夹id查找文件
                    start(HxLDRepositoryContentActivity.this, repoContent.getId(), repoContent.getName());
                }

            }
        });

    }

    @Override
    protected void requestData() {
        apiFindTablePlan();
    }

    private void apiFindTablePlan() {
        updateLoadStatus(LOADING);
        Call<RepoContentDTO> call = HxTransmitRequestHelper.getRequestApi().getFindTypeNodeAll(repoId);
        autoClose(call);
        call.enqueue(new Callback<RepoContentDTO>() {

            @Override
            public void onResponse(Call<RepoContentDTO> call, Response<RepoContentDTO> response) {
                RepoContentDTO contentDTO = response.body();
                Log.e("lwjing", contentDTO.toString());
                if (contentDTO != null && contentDTO.isSuccessful()) {
                    updateLoadStatus(LOAD_SUCCESS);
                    RepoContentDTO.DataBean data = contentDTO.getData();
                    if (data == null) {
                        ldRepoContentAdapter.setEmptyView(emptyView);
                        return;
                    }
                    //文件
                    List<RepoContentDTO.RepoContent> files = data.getFiles();
                    //文件夹
                    List<RepoContentDTO.RepoContent> folders = data.getFolders();
                    contentDatas.addAll(folders);
                    contentDatas.addAll(files);
                    if (contentDatas.size() == 0) {
                        ldRepoContentAdapter.setEmptyView(emptyView);
                    } else {
                        ldRepoContentAdapter.setNewData(contentDatas);
                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<RepoContentDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    class LDRepoContentAdapter extends BaseQuickAdapter<RepoContentDTO.RepoContent, BaseViewHolder> {


        public LDRepoContentAdapter(int layoutResId, @Nullable List<RepoContentDTO.RepoContent> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, RepoContentDTO.RepoContent repoCotent) {
            TextView tv_title = helper.getView(R.id.tv_repo_content_title);
            ImageView iv_file_type = helper.getView(R.id.iv_file_type);
            String textType = repoCotent.getTexttype();
            if ("docx".equals(textType) || "doc".equals(textType)) {
                iv_file_type.setImageResource(R.drawable.icon_word);
            } else if ("txt".equals(textType)) {
                iv_file_type.setImageResource(R.drawable.icon_txt);
            } else if ("xlsx".equals(textType) || "xls".equals(textType)) {
                iv_file_type.setImageResource(R.drawable.icon_xlsx);
            } else if ("pdf".equals(textType)) {
                iv_file_type.setImageResource(R.drawable.icon_pdf);
            } else if (repoCotent.getType() == 1) {
                //文件夹
                iv_file_type.setImageResource(R.drawable.icon_file);
            } else {
                //默认图表
                iv_file_type.setImageResource(R.drawable.icon_file_other);
            }

            if (repoCotent.getType() == 0) {
                //文件
                tv_title.setText(repoCotent.getTitle());
            } else {
                //文件夹
                tv_title.setText(repoCotent.getName());
            }
        }


    }
}
